package ij.umasuo.gui.laserCtr.epi;

import ij.IJ;
import ij.umasuo.MyConstants;
import ij.umasuo.MyResource;
import ij.umasuo.MyUserSettings;
import ij.umasuo.AOTFController.EPICtr;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * EPI laser JPanel, we need this to set the EPI's configure
 * @author liuquan
 * @date 2013/08/09
 * @version 1.0
 *
 */
public class EPIEditor extends JPanel implements ItemListener,ChangeListener, ActionListener{

	private static final long serialVersionUID = 1L;

	private static EPIEditor instance = null;

	EPICtr epiCtr = EPICtr.getInstance();
	private boolean isLaserON = false;
	//the lasers we need to this 
	
	private JCheckBox laser405CB = new JCheckBox("405");
	private JCheckBox laser459CB = new JCheckBox("459");
	private JCheckBox laser488CB = new JCheckBox("488");
	private JCheckBox laser532CB = new JCheckBox("532");
	private JCheckBox laser561CB = new JCheckBox("561");
	private JCheckBox laser656CB = new JCheckBox("656");
	
	private JSlider slider405 = new JSlider();
	private JSlider slider459 = new JSlider();
	private JSlider slider488 = new JSlider();
	private JSlider slider532 = new JSlider();
	private JSlider slider561 = new JSlider();
	private JSlider slider656 = new JSlider();
	
	private JLabel label405 = new JLabel("0%");
	private JLabel label459 = new JLabel("0%");
	private JLabel label488 = new JLabel("0%");
	private JLabel label532 = new JLabel("0%");
	private JLabel label561 = new JLabel("0%");
	private JLabel label656 = new JLabel("0%");
	
	private JButton ctrBtn = new JButton();
	
	private boolean isInit =true; 
	/**
	 * Create the panel
	 */
	private EPIEditor() {
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent arg0) {
				resized();
			}
		});
//		setBackground(UIManager.getColor("Button.background"));
		this.setToolTipText("This is the EPI setting");
		this.setBorder(new EmptyBorder(0, 0, 0, 0));

		this.setBounds(1, 1, 380, 252);
		this.setLayout(null);
		laser405CB.addItemListener(this);
		laser405CB.setBounds(20, 20, 55, 25);
		slider405.setValue(0);
		add(laser405CB);
		laser459CB.addItemListener(this);
		laser459CB.setBounds(20, 50, 55, 25);
		slider459.setValue(0);
		add(laser459CB);
		laser488CB.addItemListener(this);
		laser488CB.setBounds(20, 80, 55, 25);
		slider488.setValue(0);
		add(laser488CB);
		laser532CB.addItemListener(this);
		laser532CB.setBounds(20, 110, 55, 25);
		slider532.setValue(0);
		add(laser532CB);
		laser561CB.addItemListener(this);
		laser561CB.setBounds(20, 140, 55, 25);
		slider561.setValue(0);
		add(laser561CB);
		laser656CB.addItemListener(this);
		laser656CB.setBounds(20, 170, 55, 25);
		slider656.setValue(0);
		add(laser656CB);
		
		
		
		slider405.addChangeListener(this);
		slider405.setBounds(81, 22, 200, 23);
		add(slider405);
		slider459.addChangeListener(this);
		slider459.setBounds(81, 50, 200, 23);
		add(slider459);
		slider488.addChangeListener(this);
		slider488.setBounds(81, 81, 200, 23);
		add(slider488);
		slider532.addChangeListener(this);
		slider532.setBounds(81, 110, 200, 23);
		add(slider532);
		slider561.addChangeListener(this);
		slider561.setBounds(81, 142, 200, 23);
		add(slider561);
		slider656.addChangeListener(this);
		slider656.setBounds(81, 172, 200, 23);
		add(slider656);

		label405.setBounds(291, 25, 50, 15);
		add(label405);
		label459.setBounds(291, 55, 50, 15);
		add(label459);
		label488.setBounds(291, 85, 50, 15);
		add(label488);
		label532.setBounds(291, 115, 50, 15);
		add(label532);
		label561.setBounds(291, 145, 50, 15);
		add(label561);
		label656.setBounds(291, 175, 50, 15);
		add(label656);
		
		
		
		ctrBtn.addActionListener(this);
		ctrBtn.setHorizontalTextPosition(JLabel.CENTER);
		ctrBtn.setBounds(60, 208, 170, 40);
		ctrBtn.setBorder(null);
		ctrBtn.setIcon(new ImageIcon(MyResource.getGreenButtonBG()));
		ctrBtn.setText("Laser ON");
		ctrBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		add(ctrBtn);
		readFromSettings();
	}
	
	
	/**
	 * save settings to UserSetting
	 */
	public void saveSettings(){
		if(isInit)return;
		//clear all the old epi laser settings
		MyUserSettings.epiLaserInt.clear();
		MyUserSettings.epiLasers.clear();
		//add the new settings to the settings
		if(laser405CB.isSelected()){
			MyUserSettings.addEpiCtr("405", slider405.getValue()*MyConstants.LASER_MAX_POWER/100 + "");
		}
		if(laser459CB.isSelected()){
			MyUserSettings.addEpiCtr("459", slider459.getValue()*MyConstants.LASER_MAX_POWER/100 + "");
		}
		if(laser488CB.isSelected()){
			MyUserSettings.addEpiCtr("488", slider488.getValue()*MyConstants.LASER_MAX_POWER/100 + "");
		}
		if(laser532CB.isSelected()){
			MyUserSettings.addEpiCtr("532", slider532.getValue()*MyConstants.LASER_MAX_POWER/100 + "");
		}
		if(laser561CB.isSelected()){
			MyUserSettings.addEpiCtr("561", slider561.getValue()*MyConstants.LASER_MAX_POWER/100 + "");
		}
		if(laser656CB.isSelected()){
			MyUserSettings.addEpiCtr("656", slider656.getValue()*MyConstants.LASER_MAX_POWER/100 + "");
		}
		MyUserSettings.saveSettings();
	}
	
	/**
	 * read the usersettings and show them in the window
	 */
	public void readFromSettings() {
		// create new lasers with the settings
		for (int i = 0; i < MyUserSettings.epiLasers.size(); i++) {
			int laser = Integer.parseInt(MyUserSettings.epiLasers.get(i));
			int power = Integer.parseInt(MyUserSettings.epiLaserInt.get(i));
			switch (laser) {
				case 405: {
					laser405CB.setSelected(true);
					slider405.setValue(power * 100 / MyConstants.LASER_MAX_POWER);
					label405.setText(power*100 / MyConstants.LASER_MAX_POWER + "%");
					break;
				}
				case 459:{
					laser459CB.setSelected(true);
					slider459.setValue(power * 100 / MyConstants.LASER_MAX_POWER);
					label459.setText(power*100 / MyConstants.LASER_MAX_POWER + "%");
					break;
				}
				case 488:{
					laser488CB.setSelected(true);
					slider488.setValue(power * 100 / MyConstants.LASER_MAX_POWER);
					label488.setText(power*100 / MyConstants.LASER_MAX_POWER + "%");
					break;
				}
				case 532:{
					laser532CB.setSelected(true);
					slider532.setValue(power * 100 / MyConstants.LASER_MAX_POWER);
					label532.setText(power*100 / MyConstants.LASER_MAX_POWER + "%");
					break;
				}
				case 561:{
					laser561CB.setSelected(true);
					slider561.setValue(power * 100 / MyConstants.LASER_MAX_POWER);
					label561.setText(power*100 / MyConstants.LASER_MAX_POWER + "%");
					break;
				}
				case 656:{
					laser656CB.setSelected(true);
					slider656.setValue(power * 100 / MyConstants.LASER_MAX_POWER);
					label656.setText(power*100 / MyConstants.LASER_MAX_POWER + "%");
					break;
				}
			}
		}
		isInit = false;
	}

	/**
	 * resize the component and reorder the UI
	 */
	public void resized(){
		int width = this.getWidth();
		if(width < 380){
			width = 380;
		}
		slider405.setSize(width- 175, 23);
		label405.setLocation(width - 90, label405.getLocation().y);
		slider459.setSize(width- 175, 23);
		label459.setLocation(width - 90, label459.getLocation().y);
		slider488.setSize(width- 175, 23);
		label488.setLocation(width - 90, label488.getLocation().y);
		slider532.setSize(width- 175, 23);
		label532.setLocation(width - 90, label532.getLocation().y);
		slider561.setSize(width- 175, 23);
		label561.setLocation(width - 90, label561.getLocation().y);
		slider656.setSize(width- 175, 23);
		label656.setLocation(width - 90, label656.getLocation().y);
		
		ctrBtn.setLocation((width-170)/2, label656.getLocation().y+30);
	}

	/**
	 * use for the check box
	 */
	@Override
	public void itemStateChanged(ItemEvent e) {
		this.saveSettings();//here we save the settings all the settings are auto saved
		epiCtr.refreshLaser();//here we refresh the lasers
	}


	/**
	 * use for the slider
	 */
	@Override
	public void stateChanged(ChangeEvent e) {
		JSlider tmp = (JSlider)e.getSource();
		int power = tmp.getValue();
		if(tmp.equals(slider405)){
			label405.setText(power + "%");
		}else if(tmp.equals(slider459)){
			label459.setText(power + "%");
		}else if(tmp.equals(slider488)){
			label488.setText(power + "%");
		}else if (tmp.equals(slider532)){
			label532.setText(power + "%");
		}else if(tmp.equals(slider561)){
			label561.setText(power + "%");
		}else if(tmp.equals(slider656)){
			label656.setText(power + "%");
		}else{
			IJ.log("Wrong laser in EPI!");
		}
		this.saveSettings();//here we save the settings all the settings are auto saved
		epiCtr.refreshLaser();//here we refresh the lasers
		this.repaint();
	}
	
	public static EPIEditor getInstance(){
		if(instance == null){
			instance = new EPIEditor();
		}
		return instance;
	}


	/**
	 * used to answer the button click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		//here we call the start or stop the laser
		if(isLaserON){
			//TODO stop the laser
			epiCtr.stop();
			ctrBtn.setIcon(new ImageIcon(MyResource.getGreenButtonBG()));
			
			ctrBtn.setText("Laser OFF");
			isLaserON = false;
		}else{
			epiCtr.start();
			ctrBtn.setIcon(new ImageIcon(MyResource.getRedButtonBG()));
			ctrBtn.setText("Laser ON");
			isLaserON = true;
		}
		this.repaint();
	}
}